package com.example.myapplication.chap07;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

//【例7-2】从Web 服务器读取图像文件
public class WebImgActivity extends AppCompatActivity implements View.OnClickListener {
    ImageView img;
    TextView txt1, txt2;
    Button connBtn;

    HttpURLConnection conn = null;
    InputStream inStrem = null;
    // 练习的图片来自网络, 可更换
    String str = "https://www.gzist.edu.cn/images/logo.png";
    //    String str = "https://www.gzist.edu.cn/images/list-banner.png";
    HHandler mHandler = new HHandler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_07_webimg);
        img = findViewById(R.id.img);
        txt1 = findViewById(R.id.txt1);
        txt2 = findViewById(R.id.txt2);
        connBtn = findViewById(R.id.button);
        connBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        StrictMode.setThreadPolicy(
                new StrictMode
                        .ThreadPolicy
                        .Builder()
                        .detectDiskReads()
                        .detectDiskWrites()
                        .detectNetwork()
                        .penaltyLog()
                        .build()
        );
        StrictMode.setVmPolicy(
                new StrictMode
                        .VmPolicy
                        .Builder()
                        .detectLeakedSqlLiteObjects()
                        .detectLeakedClosableObjects()
                        .penaltyLog()
                        .penaltyDeath()
                        .build()
        );
        getPicture();
    }


    private void getPicture() {
        try {
            URL url = new URL(str);
            conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("GET");
            if (conn.getResponseCode() == 200) {
                txt1.setText("(1)建立输入流成功！");
                inStrem = conn.getInputStream();
                Bitmap bmp = BitmapFactory.decodeStream(inStrem);
                mHandler.obtainMessage(0, bmp).sendToTarget();
                inStrem.close();
            }
        } catch (Exception e2) {
            txt1.setText("(3)IO流失败");
        }
    }

    class HHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            txt2.setText("(2)下载图像成功!");
            img.setImageBitmap((Bitmap) msg.obj);
        }
    }
}

